﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Policy;
using BrokerBase.Lib.Product;
using NHibernate;
using BrokerBase.Lib.Base;
using BrokerBase.Lib.Tool;

namespace Broker.View.PolicyView
{
    /// <summary>
    /// PolicyLightForm.xaml 的交互逻辑
    /// </summary>
    public partial class PolicyLightForm 
    {
        private IPolicyLight policyLight;
        private IPolicyLight original;

        public bool IsCommit { get; set; }

        public PolicyLightForm(IPolicyLight policyLight)
        {
            InitializeComponent();
            this.original = policyLight;

            this.policyLight = new PolicyLight();

            BaseTools.CopyTo<IPolicyLight>(original, this.policyLight);

            Loaded += new RoutedEventHandler(PolicyLightForm_Loaded);
            PreviewClosed += new EventHandler<Telerik.Windows.Controls.WindowPreviewClosedEventArgs>(PolicyLightForm_PreviewClosed);
        }

        void PolicyLightForm_PreviewClosed(object sender, Telerik.Windows.Controls.WindowPreviewClosedEventArgs e)
        {
            if (IsCommit)
            {
                //policyLight.ProductType = rbtBasic.IsChecked == true ? ProductType.Basic : ProductType.Extra;
                policyLight.InsurancePeriodDescription = PolicyUtil.GetInsurancePeriodDescription(policyLight);
                policyLight.PaymentPeriodDescription = PolicyUtil.GetPaymentPeriodDescription(policyLight);

                if (Verfiy())
                {
                    BaseTools.CopyTo<IPolicyLight>(this.policyLight, original);
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        void PolicyLightForm_Loaded(object sender, RoutedEventArgs e)
        {
            InitEnumData();
            LoadTimeUnit();

            DataContext = this.policyLight;

            cmbPaymentTimeUnit.IsEnabled = false;//目前只支持年付

            if (policyLight.ProductType == ProductType.Basic)
            {
                rbtBasic.IsChecked = true;
            }
            else
            {
                rbtExtra.IsChecked = true;
            }
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = true;
            Close();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = false;
            Close();
        }


        private void LoadTimeUnit()
        {
            ISession sess = LibGlobal.HbmSessionFactory.GetCurrentSession();

            IList<ITimeUnit> tus = sess.CreateQuery("from TimeUnit").List<ITimeUnit>();

            cmbPaymentTimeUnit.ItemsSource = tus;
        }

        private void InitEnumData()
        {
            InsurancePeriodStyleClass[] insStyles = new InsurancePeriodStyleClass[2]
            {
                new InsurancePeriodStyleClass {Name ="期间",PeriodStyle=InsurancePeriodStyle.Period},
                new InsurancePeriodStyleClass{Name="到年龄",PeriodStyle=InsurancePeriodStyle.Age}
                
            };

            cmbInsurancePeriodType.ItemsSource = insStyles;

            PaymentPeriodStyleClass[] payStyles = new PaymentPeriodStyleClass[2]
            {
                new PaymentPeriodStyleClass{Name="期间",PeriodStyle =PaymentPeriodStyle.Period},
                new PaymentPeriodStyleClass{Name ="到年龄",PeriodStyle=PaymentPeriodStyle.Age}
                
            };

            cmbPaymentPeriodType.ItemsSource = payStyles;

            UnitTypeClass[] unitTypes = new UnitTypeClass[2]
            {
                new UnitTypeClass{Name="金额", UnitType=UnitType.Amt},
                new UnitTypeClass{Name="数量",UnitType =UnitType.Qty}
            };

            cmbUnitType.ItemsSource = unitTypes;
        }

        private bool Verfiy()
        {

            if (string.IsNullOrEmpty(policyLight.ProductName))
            {
                MessageBox.Show("产品名称不能为空!", "提示");
                return false;
            }

            if ((policyLight.InsurancePeriodStyle == InsurancePeriodStyle.Age || policyLight.InsurancePeriodStyle == InsurancePeriodStyle.Period) && policyLight.InsurancePeriodValue == 0)
            {
                MessageBox.Show("保险期限类型等于[年龄],[期间]时，保险期限不能等于空", "提示");
                return false;
                
            }

            if ((policyLight.PaymentPeriodStyle == PaymentPeriodStyle.Age || policyLight.PaymentPeriodStyle == PaymentPeriodStyle.Period) && policyLight.PaymentPeriodValue == 0)
            {
                MessageBox.Show("付款期限类型等于[年龄],[期间]时，付款期限不能等于空", "提示");
                return false;

            }

            if (policyLight.PaymentPeriodLength > policyLight.InsurancePeriodLength)
            {
                MessageBox.Show("付款期不能大于保险期","提示");
                return false;
            }

            if (policyLight.SelectedTimeUnit == null)
            {
                MessageBox.Show("付款周期不能为空!", "提示");
                return false;
            }

            IPolicyLight bpl = policyLight.Owner.BasicPolicyLight;
            if (policyLight.ProductType==ProductType.Basic && bpl!=null && bpl.Id !=policyLight.Id )
            {
                MessageBox.Show("一个保险方案只能有一个主险！", "提示");
                return false;
            }


            return true;
        }
    }

    //class InsurancePeriodStyleClass
    //{
    //    public string Name { get; set; }

    //    public InsurancePeriodStyle PeriodStyle { get; set; }
    //}

    //class PaymentPeriodStyleClass
    //{
    //    public string Name { get; set; }

    //    public PaymentPeriodStyle PeriodStyle { get; set; }
    //}

    //class UnitTypeClass
    //{
    //    public string Name { get; set; }

    //    public UnitType UnitType { get; set; }
    //}
}
